{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import IFrame\n",
    "documentation = IFrame(src='https://ipywidgets.readthedocs.io/en/latest/',height=400,width=1500)\n",
    "display(documentation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipycanvas import __version__\n",
    "print(__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from ipycanvas import Canvas\n",
    "import ipywidgets as widgets\n",
    "\n",
    "class BlackBoard(Canvas):\n",
    "    last=None\n",
    "    draw=False\n",
    "    def __init__(self, *args, **kwargs):\n",
    "        super().__init__(width=28, height=28,sync_image_data=True,*args, **kwargs)\n",
    "        self.stroke_style = 'black'\n",
    "        self.on_mouse_move(self.handle_mouse_move)\n",
    "        self.on_mouse_down(self.handle_mouse_down)\n",
    "        self.on_mouse_up(self.handle_mouse_up)\n",
    "        self.layout.width=\"200px\"\n",
    "        self.layout.border='solid'\n",
    "    def handle_mouse_move(self,x, y):\n",
    "        if self.draw:\n",
    "            self.stroke_line(self.last[0],self.last[1],x,y)\n",
    "            self.last=(x,y)\n",
    "    def handle_mouse_down(self,x,y):\n",
    "        self.draw=True\n",
    "        self.last=(x,y)\n",
    "    def handle_mouse_up(self,x,y):\n",
    "        self.draw=False\n",
    "    def predict(self):\n",
    "        print(self.blackboard.get_image_data())\n",
    "class Plane:\n",
    "    blackboard=BlackBoard()\n",
    "    clear_board_button=widgets.Button(\n",
    "        description='清除白板',\n",
    "        disabled=False,\n",
    "        button_style='', # 'success', 'info', 'warning', 'danger' or ''\n",
    "        tooltip='Clear board',\n",
    "    )\n",
    "    clear_output_button=widgets.Button(\n",
    "        description='清除输出',\n",
    "        disabled=False,\n",
    "        button_style='',\n",
    "        tooltip='Clear output',\n",
    "    )\n",
    "    submit_button=widgets.Button(\n",
    "        description='提交',\n",
    "        disabled=False,\n",
    "        button_style='',\n",
    "        tooltip='submit',\n",
    "        icon='check'\n",
    "    )\n",
    "    out=widgets.Output(layout={'border': '1px solid black'})\n",
    "    def __init__(self):\n",
    "        self.clear_board_button.on_click(self.clear_button_click)\n",
    "        self.clear_output_button.on_click(self.clear_output_click)\n",
    "        self.submit_button.on_click(self.submit)\n",
    "    def clear_button_click(self,event):\n",
    "        self.blackboard.clear()\n",
    "    def clear_output_click(self,event):\n",
    "        self.out.clear_output()\n",
    "    def submit(self,event):\n",
    "        with self.out:\n",
    "            print(self.blackboard.image_data)\n",
    "    def render(self):\n",
    "        return widgets.VBox([widgets.HBox([self.blackboard,self.submit_button,self.clear_board_button,self.clear_output_button]),self.out])\n",
    "    def predict(self):\n",
    "        with self.out:\n",
    "            print(self.blackboard.image_data())\n",
    "Plane().render()\n",
    "# 暂时无法获取输出，另寻他法 sync_image_data 不能再vs jupyter_botebook中运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import *\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "x = np.linspace(0, 2 * np.pi)\n",
    "\n",
    "def update(w = 1.0):\n",
    "    fig = plt.figure()\n",
    "    ax = fig.add_subplot(1, 1, 1)\n",
    "    ax.plot(x, np.sin(w * x))\n",
    "\n",
    "    fig.canvas.draw()\n",
    "\n",
    "interact(update);"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "586c3cdf86f3a3a1181be60287eeea244d671c39d2199c05edac4d89c647599f"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
